package com.keldian.zhbj74.fragment;

import java.util.ArrayList;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.keldian.zhbj74.R;
import com.keldian.zhbj74.activity.MainActivity;
import com.keldian.zhbj74.base.impl.NewsCenterPager;
import com.keldian.zhbj74.domain.NewsMenu;
import com.keldian.zhbj74.domain.NewsMenu.NewsMenuData;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class LeftMenuFragment extends BaseFragment {

	private ListView mMenuList;

	private int mCurrentPos;
	private MenuAdapter menuAdapter;

	@Override
	public View initView() {
		View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
		mMenuList = (ListView) view.findViewById(R.id.lv_list);

		return view;
	}

	@Override
	public void initData() {

	}

	public void setMenuData(ArrayList<NewsMenuData> data) {
//		mCurrentPos = 0;
		menuAdapter = new MenuAdapter(data);
		mMenuList.setAdapter(menuAdapter);

		mMenuList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCurrentPos = position;
				menuAdapter.notifyDataSetChanged();

				toggle();

				setCurrentDetailPager(position);
			}

		});
	}

	private void setCurrentDetailPager(int position) {

		// 获取新闻中心的对象
		MainActivity mainUI = (MainActivity) mActivity;
		// 获取ContentFragment
		ContentFragment fragment = mainUI.getContentFragment();
		// 获取NewsCenterPager
		NewsCenterPager newsCenterPager = fragment.getNewsCenterPager();
		// 修改新闻中心的FrameLayout的布局
		newsCenterPager.setCurrentDetailPager(position);

	}

	private void toggle() {

		MainActivity mainUI = (MainActivity) mActivity;
		SlidingMenu slidingMenu = mainUI.getSlidingMenu();
		slidingMenu.toggle();
	}

	class MenuAdapter extends BaseAdapter {

		public ArrayList<NewsMenuData> menuData;

		public MenuAdapter(ArrayList<NewsMenuData> data) {
			super();
			this.menuData = data;
		}

		@Override
		public int getCount() {
			return menuData.size();
		}

		@Override
		public NewsMenuData getItem(int position) {
			return menuData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = View.inflate(mActivity, R.layout.list_item_left_menu,
					null);
			TextView tvMenu = (TextView) view.findViewById(R.id.tv_menu);

			NewsMenuData item = getItem(position);
			tvMenu.setText(item.title);

			if (position == mCurrentPos) {
				// 被选中
				tvMenu.setEnabled(true);// 文字变为红色
			} else {
				// 未选中
				tvMenu.setEnabled(false);// 文字变为白色
			}

			return view;
		}

	}

}
